package com.ydsungan.worldbook.service.impl;


import com.ydsungan.worldbook.dao.UserDao;
import com.ydsungan.worldbook.pojo.User;

import com.ydsungan.worldbook.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Transactional
    @Override
    public boolean register(User user) {
        if (user.getUsername() != null && !"".equals(user.getUsername())){
            user.setCreateTime(new Date());
            try{
                int effectedNum = userDao.register(user);
                if(effectedNum > 0){
                    return true;
                }else{
                    throw new RuntimeException("新用户注册失败!");
                }
            }catch(Exception e){
                throw new RuntimeException("新用户注册失败:" + e.getMessage());
            }
        }else{
            throw new RuntimeException("注册用户信息不能为空! ");
        }

    }
    @Override
    public boolean updateUser(User user) {

        int effectedNum = userDao.updateUser(user);
        if (effectedNum > 0){
            return true;
        }else{
            logger.info("更新用户信息失败");
            throw new RuntimeException("更新用户信息失败");
        }
    }

    @Override
    public List<User> getUserByMap(Map<String, Object> map) {
        return userDao.getUserByMap(map);
    }
}
